<?php

namespace app\admin\controller;

use app\admin\model\User as Users;
use app\admin\validate\User as UserValidate;
use think\App;
use think\Db;
use think\Request;

class User extends Common
{
    protected function initialize()
    {
        parent::initialize();
        $this->m = new Users;   //别名：避免与控制名冲突
        $this->v = new UserValidate;   //别名：避免与控制名冲突
    }

    public function index()
    {
        return $this->fetch();
    }

    public function add()
    {
        if (request()->isPost()) {
            $data = input('post.');
            if (!$this->v->scene('add')->check($data)) {
                return json(['code' => 404, 'msg' => $this->v->getError(), 'data' => null]);
            }
            $result = $this->m->create($data);
            $result->userGroup()->saveAll($data['groups']);
            if ($result) {
                return json(['code' => 0, 'msg' => '成功', 'data' => null]);
            } else {
                return json(['code' => 0, 'msg' => $this->m->getError(), 'data' => null]);
            }
        }
        $groups=model('AuthGroup')->all();
        $this->assign(compact('groups'));
        return $this->fetch();
    }

    public static function Menu(){
        $user=Users::with('userGroup')->get(UID);
        $rules=[];
        foreach ($user['userGroup'] as $group){
            $rules=array_merge($rules,explode(',',$group['rules']));
        }
       $menus= \app\admin\model\AuthRule::getMenu($rules);
       return json(["status"=>0,"msg"=>"ok","data"=>$menus]);
    }
    public function tree($page = 1, $limit = 10)
    {
        return json([
            'code'=>0,
            'msg'=>'查询成功',
            'count'=>$this->m->count(),
            'data'=>$this->m->with(['userGroup'=>function($query){
                return $query->where('auth_group.id',1);
            }])->page($page,$limit)->select()
        ]);
        return $this->m->with(['userGroup'=>function($query){
            return $query->where('id',1)->filed('title');
        }])->pageTable($page, $limit);
    }

    /*
     * 用户用户id获取选中的权限节点list
     */
    public function getUserRule($id)
    {
        $user = $this->m->get($id);
        $rule = new \app\admin\model\AuthRule();
        return json($rule->getUserRule($user['rules']));

    }

    public function edit($id)
    {
        if ($this->request->isPost()) {
            $data = $this->request->post();
            if (!$this->v->scene('edit')->check($data)) {
                return json(['code' => 404, 'msg' => $this->v->getError(), 'data' => null]);
            }
            $user = Users::get($id);
            $result = $user->save($data);
            if ($result) {
                return json(['code' => 0, 'msg' => '成功', 'data' => null]);
            } else {
                return json(['code' => 0, 'msg' => $this->m->getError(), 'data' => null]);
            }
        } else {
            $user=$this->m->with('userGroup')->get($id);
            $group_checked_id=array_column($user->userGroup->toArray(),'id');//获取所有选中的id
            $groups=model('AuthGroup')->all();
            foreach ($groups as &$g){
                if(in_array($g->id,$group_checked_id)){//判断当前是否选中
                    $g->checked='checked';
                }else{
                    $g->checked='';
                }
            }
            $this->assign(compact('user','groups'));
            return $this->fetch();
        }
    }

    /**
     * 修改密码
     * @param Request $request
     * @param $id
     * @return \think\response\Json
     */
    public function password(Request $request, $id)
    {
        if ($request->isPost()) {
            $data = $request->post();
            if (!$this->v->scene('password')->check($data)) {
                return json(['code' => 404, 'msg' => $this->v->getError(), 'data' => null]);
            }
            $user = Users::get($id);
            $result = $user->save($data);
            if ($result) {
                return json(['code' => 0, 'msg' => '成功', 'data' => null]);
            } else {
                return json(['code' => 0, 'msg' => '失败','data'=> null]);
            }
        }
    }

    /*
     * ajax更新部分数据
     */
    public function ajax($id)
    {
        return parent::_ajax($id);
    }

    public function delete($id)
    {
        return parent::_delete($id);
    }
}